package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Service
public class ItemServiceImpl implements ItemService {

    //做数据汇总：service-product 这个微服务
    //需要使用远程调用

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getItem(Long skuId) {
        Map<String, Object> map = new HashMap<>();
        /*
            1. 查询sku的基本信息
            2. 查询skuImage
            3. 分类信息
            4. spu的销售属性，属性值
            5. 查询最新价格
            6. 实现切换功能
         */
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            // 根绝skuId火舞skuInfo数据放入map中
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo",skuInfo);
            return skuInfo;
        });

        // 根据分类id查询分类数据
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        });

        // 获取销售属性，属性值
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrList);
        });

        // 获取最新价格
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        });

        //切换数据
        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            //将map转成json
            String mapJson = JSON.toJSONString(skuValueIdsMap);
            map.put("valuesSkuJson", mapJson);
        });

        // 调用商品热度排名数据接口
        CompletableFuture<Void> hotCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        });

        CompletableFuture.allOf(skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                spuSaleAttrListCompletableFuture,
                priceCompletableFuture,
                valuesSkuJsonCompletableFuture,
                hotCompletableFuture).join();



//        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
//        if(skuInfo!=null) {
//            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
//            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
//            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
//            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
//            //将map转成json
//            String mapJson = JSON.toJSONString(skuValueIdsMap);
//
//
//            //需要将数据存储到map中
//            map.put("skuInfo",skuInfo);
//            map.put("categoryView",categoryView);
//            map.put("spuSaleAttrList",spuSaleAttrList);
//            map.put("price",skuPrice);
//            map.put("valuesSkuJson",mapJson);
//        }


        return map;
    }
}
